<?php
include_once "IDbDriver.php";
class MySqlDriver extends Base implements IDbDriver
{
    private $_db = null;
    private $_query = null;
    private $_sql = null;

    public function __construct()
    {
        $this->_db = mysql_connect($this->_buildDSN(),C('db=>user'),C('db=>pwd'));
        if(!$this->_db) {
            Logger::log('[Error] can not connect to the database server by mysql drive,the error is :'.mysql_error());
            return ;
        }
        if(!mysql_select_db(C('db=>dbname'),$this->_db)) {
            Logger::log('[Error] fail to select the db,the error is:'.mysql_error());
            return ;
        }
        mysql_query('set names "UTF8";');

    }

    public function close()
    {
        if (null !== $this->_db) {
            mysql_close($this->_db);
        }
    }

    /**
     * 释放查询
     * @access protected
     * @param void
     * @return void
     */
    protected function _free() {
        mysql_free_result($this->_query);
        $this->_query = null;
    }

    /**
     * 生成一个pdo的dsn
     * @return mixed
     */
    private function _buildDSN(){
        //func_get_args — Returns an array comprising a function's argument list
        //$args = func_get_args();
        //call_user_func_array — Call a callback with an array of parameters
        return call_user_func_array('sprintf', array('%s:%s',
            C('db=>host') ? C('db=>host') : 'localhost',
            C('db=>port') ? C('db=>port') : '3306'
        ));
    }

    public function prepare($sql)
    {
        Logger::log("[Info] sql  : " . $sql);
        if (null !== $this->_sql) {
            $this->_sql = null;
        }
        $this->_sql = $sql;
    }

    public function execute()
    {
        if (null === $this->_db) {
            $this->connect();
        }

        if(null !== $this->_query) {
            $this->_free();
        }
		
        $this->_query = mysql_query($this->_sql,$this->_db);
        if($this->_query) {
            Logger::log("执行了execute()函数,SQL为:".$this->_sql);
        } else {
            Logger::log("执行了execute()函数,SQL为:".$this->_sql.",错误代码为:".mysql_errno().",错误信息为:".mysql_error());
        }

        return $this->_query;
    }

    public function getAllByAssocArray()
    {
        $tmpLen = 0;
        $this->_queryResult = array();
        while($arr = mysql_fetch_array($this->_query,MYSQL_ASSOC)) {
            $this->_queryResult[] = $arr;
            $tmpLen ++;
        }
        $this->_queryNum = $tmpLen;

        return $this->_queryResult;
    }

    /**
     * 开始事务
     */
    public function beginTrans()
    {
        $this->connect();
        mysql_query('SET AUTOCOMMIT = 0',$this->_db);
        mysql_query('BEGIN',$this->_db);
    }

    public function commit()
    {
        mysql_query('COMMIT',$this->_db);
    }

    public function rollback()
    {
        mysql_query('ROLL BACK',$this->_db);
    }

    /**
     * Version number query string
     *
     * @access	public
     * @return	string
     */
    public function version(){
        return "SELECT version() AS ver";
    }

}